package com.elinkway.infinitemovies.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ScrollView;

import com.elinkway.infinitemovies.async.RequestResultListener;
import com.elinkway.infinitemovies.bean.FeedbackQuestionBean;
import com.elinkway.infinitemovies.bean.ReportSubmitBean;
import com.elinkway.infinitemovies.http.requesttask.SubmitFeedbackQuestionTask;
import com.elinkway.infinitemovies.utils.MyLocationManager;
import com.elinkway.infinitemovies.utils.SharePreferenceManager;
import com.elinkway.infinitemovies.utils.ToastUtil;
import com.le123.ysdq.R;

import java.util.ArrayList;

public class AdviseActivity extends BaseActionBarActivity implements View.OnClickListener, View.OnFocusChangeListener{

    private ScrollView mContentScroll;
    private EditText mAdvise;
    private EditText mContact;
    private Button mSubmit;
    private String mAid;
    private String mSite;
    private String mScope;
    private ViewTreeObserver.OnGlobalLayoutListener mOnGlobalLayoutListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_advise);
        initView();
        initData();
    }

    private void initView() {
        mContentScroll = (ScrollView) findViewById(R.id.content_scroll);
        mAdvise = (EditText) findViewById(R.id.description);
        mContact = (EditText) findViewById(R.id.contact);
        mSubmit = (Button) findViewById(R.id.submit);
        initActionBar();
        mTitleTv.setText(getString(R.string.feedback));
        mSubmit.setOnClickListener(this);
        mContact.setOnFocusChangeListener(this);
        mOnGlobalLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                mContentScroll.fullScroll(ScrollView.FOCUS_DOWN);
            }
        };
    }

    private void initData() {
        Intent intent = getIntent();
        mAid = intent.getStringExtra(FeedbackSubmitActivity.AID);
        mSite = intent.getStringExtra(FeedbackSubmitActivity.SITE);
        mScope = intent.getStringExtra(FeedbackSubmitActivity.SCOPE);
    }

    private void changeSubmitButtonState(boolean isEnable) {
        if (isEnable) {
            mSubmit.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.color_3599f8));
            mSubmit.setEnabled(true);
        } else {
            mSubmit.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.color_DDDDDD));
            mSubmit.setEnabled(false);
        }
    }

    private void submitData() {
        if (mAdvise.getText().toString().isEmpty()) {
            ToastUtil.showLongToast(this, getString(R.string.no_detail));
            return;
        }
        if (mContact.getText().toString().isEmpty()) {
            ToastUtil.showLongToast(this, getString(R.string.no_contact));
            return;
        }
        SubmitFeedbackQuestionTask requestTask = new SubmitFeedbackQuestionTask(this, mAid, mSite, SharePreferenceManager.getVaule(this, MyLocationManager.LOCATIONKEY, "CN_1_5_1"),
                mScope, "", mAdvise.getText().toString(), mContact.getText().toString());
        requestTask.setListener(new RequestResultListener<ReportSubmitBean>() {
            @Override
            public boolean onRequestFailed() {
                ToastUtil.showLongToast(getApplicationContext(), getString(R.string.feedback_fail));
                return false;
            }

            @Override
            public void onRequestSuccess(int updateId, ReportSubmitBean result) {
                ToastUtil.showLongToast(getApplicationContext(), getString(R.string.feedback_tip));
                finish();
            }

            @Override
            public void onPreRequest() {

            }
        });
        requestTask.start();
    }

    @Override
    public void dealWithUmeng() {

    }

    @Override
    public void dealWithVisitUmeng() {

    }

    public static void launchActivity(Context context, String aid, String scope) {
        Intent intent = new Intent(context, AdviseActivity.class);
        intent.putExtra(FeedbackSubmitActivity.AID, aid);
        intent.putExtra(FeedbackSubmitActivity.SCOPE, scope);
        context.startActivity(intent);
    }

    @Override
    public void onClick(View v) {
        submitData();
    }

    @Override
    public void onFocusChange(View view, boolean hasFocus) {
        if (hasFocus) {
            mContact.getViewTreeObserver().addOnGlobalLayoutListener(mOnGlobalLayoutListener);
        } else {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                mContact.getViewTreeObserver().removeOnGlobalLayoutListener(mOnGlobalLayoutListener);
            } else {
                mContact.getViewTreeObserver().removeGlobalOnLayoutListener(mOnGlobalLayoutListener);
            }

        }
    }
}
